package main

import (
	"fmt"
)

/*
*
Demo版本，缩容的部分没有实现
*/
type SliceHelper[T any] interface {
	Delete(slice []T, index int)
}
type SliceV1[T any] struct {
	slice []T
	len   int
	cap   int
}

func (s *SliceV1[T]) Delete(index int) error {
	if s.len == 0 || s.cap == 0 {
		return fmt.Errorf("slice has no elements")
	}
	if index > s.len {
		return fmt.Errorf("index is out of the slice")
	}
	copy(s.slice[index:], s.slice[index+1:])
	s.slice = s.slice[:len(s.slice)-1]
	s.len--
	if s.len < s.cap/2 {
		newSlice := make([]T, 0, s.cap/2)
		copy(newSlice, s.slice)
		s.slice = newSlice
	}
	return nil
}
func main() {
	sliceTest := &SliceV1[int]{cap: 8, len: 3}
	sliceTest.slice = make([]int, 0, 8)
	sliceTest.slice = append(sliceTest.slice, 1, 2, 3)
	if err := sliceTest.Delete(1); err != nil {
		println("something wrong: " + err.Error())
	}
	fmt.Printf("slice after delete: %+v", sliceTest)
}
